package com.hh.service.service.impl.socialize;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.common.model.socialize.UserSocializeConfig;
import com.hh.common.model.socialize.UserSocializeInfo;
import com.hh.common.model.socialize.UserSocializeView;
import com.hh.common.request.EditSocializeInfoRequest;
import com.hh.common.response.SocializeInfoResponse;
import com.hh.common.utils.DateUtil;
import com.hh.service.dao.socialize.UserSocializeConfigDao;
import com.hh.service.dao.socialize.UserSocializeInfoDao;
import com.hh.service.service.SystemAttachmentService;
import com.hh.service.service.UserService;
import com.hh.service.service.socialize.UserSocializeConfigService;
import com.hh.service.service.socialize.UserSocializeInfoService;
import com.hh.service.service.socialize.UserSocializeViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserSocializeInfoServiceImpl extends ServiceImpl<UserSocializeInfoDao,UserSocializeInfo> implements UserSocializeInfoService {
    
    @Resource
    private UserSocializeInfoDao dao;

    @Resource
    private UserSocializeConfigDao userSocializeConfigDao;

    @Autowired
    private UserService userService;

    @Autowired
    private UserSocializeViewService userSocializeViewService;

    @Autowired
    private UserSocializeConfigService userSocializeConfigService;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

//    @Override
//    public String isExistImUser(String imUserId) {
//        Map<String,Object> map = new HashMap<>();
//        if(imUserId == null){
//            List<UserSocializeInfo> userSocializeInfos = dao.selectList(null);
//            if(userSocializeInfos.size() > 0){
//                Random random = new Random();
//                int n = random.nextInt(userSocializeInfos.size());
//                String imId = userSocializeInfos.get(n).getImUid();
//                return imId;
//            }else {
//                return "35258742";
//            }
//        }else {
//            LambdaQueryWrapper<UserSocializeInfo> lwq = Wrappers.lambdaQuery();
//            lwq.eq(UserSocializeInfo::getImUid,imUserId);
//            UserSocializeInfo userSocializeInfo = dao.selectOne(lwq);
//            if(ObjectUtil.isNotNull(userSocializeInfo)){
//                return imUserId;
//            }else {
//                List<UserSocializeInfo> userSocializeInfos = dao.selectList(null);
//                if(userSocializeInfos.size() > 0){
//                    Random random = new Random();
//                    int n = random.nextInt(userSocializeInfos.size());
//                    String imId = userSocializeInfos.get(n).getImUid();
//                    return imId;
//                }else {
//                    return "35258742";
//                }
//            }
//        }
//    }

    @Override
    public SocializeInfoResponse getSocializeInfo() {
        Integer uid = userService.getUserIdException();
        UserSocializeInfo userSocializeInfo = dao.selectById(uid);
        SocializeInfoResponse socializeInfoResponse = new SocializeInfoResponse();
        UserSocializeConfig userSocializeConfig = userSocializeConfigService.getConfig();
        if(ObjectUtil.isNull(userSocializeInfo)){
            BeanUtil.copyProperties(userSocializeConfig,socializeInfoResponse);
        }else {
            BeanUtil.copyProperties(userSocializeInfo,socializeInfoResponse);
            socializeInfoResponse.setAddress(userSocializeConfig.getAddress());
            socializeInfoResponse.setDetailedDiagram(userSocializeConfig.getDetailedDiagram());
            socializeInfoResponse.setPosition(userSocializeConfig.getPosition());
            socializeInfoResponse.setVideo(userSocializeConfig.getVideo());
            socializeInfoResponse.setVoice(userSocializeConfig.getVoice());
        }
        addNumberView(uid);
        socializeInfoResponse.setViewList(getViewList());
        return socializeInfoResponse;
    }

    @Override
    public Boolean giveLike() {
        Integer uid = userService.getUserIdException();
        UserSocializeInfo userSocializeInfo = dao.selectById(uid);
        if(ObjectUtil.isNull(userSocializeInfo)){
            userSocializeConfigDao.increaseLike();
            return Boolean.TRUE;
        }else {
            dao.increaseLike(uid);
            return Boolean.TRUE;
        }
    }

    @Override
    public Boolean editInfo(EditSocializeInfoRequest request) {
        Integer userId = userService.getUserIdException();
        UserSocializeInfo userSocializeInfo = new UserSocializeInfo();
        userSocializeInfo.setUid(userId);
        userSocializeInfo.setMailbox(request.getMailbox());
        userSocializeInfo.setFullName(request.getFullName());
        userSocializeInfo.setPhone(request.getPhone());
        userSocializeInfo.setPhoto(systemAttachmentService.clearPrefix(request.getPhoto()));
        return saveOrUpdate(userSocializeInfo);
    }

    private List<String> getViewList() {
        return userSocializeViewService.getViewList();
    }

    /**
     * 增加浏览数
     * @param uid
     */
    private void addNumberView(Integer uid) {
        Date nowTime = DateUtil.nowDateTime();

        UserSocializeView userSocializeView = new UserSocializeView();
        userSocializeView.setUid(uid);
        userSocializeView.setViewTime(nowTime);
        userSocializeViewService.replaceInto(userSocializeView);
        dao.increaseView(uid);

        userSocializeConfigDao.increaseView();
    }
}
